/* eslint-disable max-lines-per-function */
import { getAppSelectList } from '/@/api/system-mgmt/app-mgmt/app';
import { getInstanceTypes } from '/@/api/system-mgmt/instance-mgmt/instance';
import { getDeployStrategy } from '/@/api/instance-purchase/purchase';
import { getPackageList } from '../../api/system-mgmt/app-mgmt/app';
import eventBus from '/@/utils/eventBus';
import { FormSchema } from '/@/components/BasicForm/type/form';

export const step1Schema: FormSchema[] = [
  {
    field: 'appId',
    label: '应用',
    required: true,
    // 对接API
    component: 'ApiSelect',
    componentProps: ({ formModel, formActionType }) => {
      return {
        api: getAppSelectList,
        labelField: 'name',
        valueField: 'id',
        immediate: true,
        placeholder: '请选择应用',
        onChange: async (e) => {
          if (e === undefined) {
            formModel.specCard = undefined;
            formModel.duration = undefined;
            return;
          }
          formModel.specCard = undefined;
          formModel.duration = undefined;
          const instanceOptions = await getInstanceTypes(e);

          const { updateSchema } = formActionType;

          updateSchema({
            field: 'type',
            componentProps: {
              options: instanceOptions,
            },
          });

          updateSchema({
            field: 'duration',
            componentProps: {
              options: [
                {
                  label: '一年',
                  value: '一年',
                },
                {
                  label: '三年',
                  value: '三年',
                },
                {
                  label: '五年',
                  value: '五年',
                },
              ],
            },
          });
        },
      };
    },
    colProps: { span: 13 },
    rules: [
      {
        trigger: 'change',
        required: true,
        message: '请选择应用',
      },
    ],
  },
  {
    field: 'type',
    label: '规格',
    component: 'InstanceRadioGroup',
    required: true,
    componentProps: {
      options: [],
    },
  },
  {
    field: 'deployStrategy',
    label: '管理面策略',
    component: 'ApiSelect',
    required: true,
    colProps: {
      span: 13,
    },
    componentProps: {
      api: getDeployStrategy,
      resultField: 'list',
      labelField: 'desc',
      valueField: 'strategy',
    },
  },
  {
    field: 'notificationEnabled',
    label: '是否接受消息通知',
    component: 'RadioGroup',
    required: true,
    colProps: {
      span: 13,
    },
    componentProps: ({ formActionType }) => {
      return {
        options: [
          { name: '是', label: '是', value: 'true' },
          { name: '否', label: '否', value: 'false' },
        ],
        onChange: async (e) => {
          const { updateSchema } = formActionType;
          updateSchema({
            field: 'notificationType',
            show: e.target.value === 'true',
            required: e.target.value === 'true',
          });
          updateSchema({
            field: 'notificationTerminal',
            show: e.target.value === 'true',
            required: e.target.value === 'true',
          });
        },
      };
    },
  },
  {
    field: 'notificationType',
    label: '通知协议类型',
    component: 'RadioGroup',
    required: false,
    colProps: {
      span: 13,
    },
    componentProps: {
      options: [
        { name: '短信', label: '短信', value: 'sms' },
        { name: '邮件', label: '邮件', value: 'email' },
      ],
    },
    show: false,
  },
  {
    field: 'notificationTerminal',
    label: '通知终端',
    component: 'Input',
    required: false,
    colProps: {
      span: 13,
    },
    componentProps: {
      placeholder: '请输入邮箱或者手机号',
    },
    show: false,
  },
  {
    field: 'paymentType',
    label: '付费类型',
    component: 'RadioGroup',
    required: true,
    colProps: {
      span: 13,
    },
    componentProps: {
      options: [
        { name: '包周期', label: '包周期', value: 'prepaid' },
        { name: '按需付费', label: '按需付费', value: 'postpaid' },
      ],
    },
  },

  {
    field: 'duration',
    component: 'InputNumber',
    label: '购买时长（月）',
    required: true,
    componentProps: {
      width: 50,
      min: 1,
    },
    show: ({ values }) => {
      return Boolean(values.paymentType === 'prepaid');
    },
    rules: [
      {
        trigger: 'change',
        required: true,
        message: '请选择购买时长',
      },
    ],
  },
];

async function getPackage(appId: string) {
  const instanceOptions = await getPackageList({ appId });
  return instanceOptions;
}

// eslint-disable-next-line max-lines-per-function
export async function genStep1Schema(isTenant: boolean, appId: string) {
  let selectedAppSchema: FormSchema[] = [];
  if (isTenant) {
    const instanceOptions = await getPackage(appId);
    selectedAppSchema = [
      {
        field: 'type',
        label: '规格',
        component: 'InstanceRadioGroup',
        required: true,
        componentProps: ({ formActionType }) => {
          const { updateSchema } = formActionType;
          return {
            options: instanceOptions,
            onChange: (e) => {
              eventBus.emit('clearFsForm');
              if (e.architecture === '单体') {
                updateSchema({
                  field: 'sfsTurboDtoList',
                  ifShow: e.ifSupportFileSystem,
                  required: false,
                });
              } else if (e.architecture === '微服务') {
                updateSchema([
                  {
                    field: 'fileSystemType',
                    ifShow: e.ifSupportFileSystem,
                    required: false,
                  },
                  {
                    field: 'mountPath',
                    ifShow: e.ifSupportFileSystem,
                    required: false,
                  },
                ]);
              }
            },
          };
        },
      },
    ];
  } else {
    selectedAppSchema = [
      {
        field: 'appId',
        label: '应用',
        required: true,
        // 对接API
        component: 'ApiSelect',
        componentProps: ({ formModel, formActionType }) => {
          return {
            api: getAppSelectList,
            labelField: 'name',
            valueField: 'id',
            immediate: true,
            placeholder: '请选择应用',
            onChange: async (e) => {
              if (e === undefined) {
                formModel.specCard = undefined;
                formModel.duration = undefined;
                return;
              }
              formModel.specCard = undefined;
              formModel.duration = undefined;
              const instanceOptions = await getInstanceTypes(e);
              const { updateSchema } = formActionType;
              eventBus.emit('clearFsForm');
              updateSchema({
                field: 'type',
                componentProps: {
                  options: instanceOptions,
                  onChange: (e) => {
                    if (e.architecture === '单体') {
                      updateSchema({
                        field: 'sfsTurboDtoList',
                        ifShow: e.ifSupportFileSystem,
                        required: false,
                      });
                    } else if (e.architecture === '微服务') {
                      updateSchema([
                        {
                          field: 'fileSystemType',
                          ifShow: e.ifSupportFileSystem,
                          required: false,
                        },
                        {
                          field: 'mountPath',
                          ifShow: e.ifSupportFileSystem,
                          required: false,
                        },
                      ]);
                    }
                  },
                },
              });
              updateSchema({
                field: 'sfsTurboDtoList',
                ifShow: false,
              });
              updateSchema([
                {
                  field: 'fileSystemType',
                  ifShow: false,
                },
                {
                  field: 'mountPath',
                  ifShow: false,
                },
              ]);
              updateSchema({
                field: 'duration',
                componentProps: {
                  options: [
                    {
                      label: '一年',
                      value: '一年',
                    },
                    {
                      label: '三年',
                      value: '三年',
                    },
                    {
                      label: '五年',
                      value: '五年',
                    },
                  ],
                },
              });
            },
          };
        },
        colProps: { span: 13 },
        rules: [
          {
            trigger: 'change',
            required: true,
            message: '请选择应用',
          },
        ],
      },
      {
        field: 'type',
        label: '规格',
        component: 'InstanceRadioGroup',
        required: true,
        componentProps: () => {
          return {};
        },
      },
      {
        field: 'deployStrategy',
        label: '管理面策略',
        component: 'ApiSelect',
        required: false,
        colProps: {
          span: 13,
        },
        componentProps: {
          api: getDeployStrategy,
          resultField: 'list',
          labelField: 'desc',
          valueField: 'strategy',
        },
      },
    ];
  }

  const tenantStep1Schema: FormSchema[] = [
    {
      field: 'sfsTurboDtoList',
      label: '文件系统',
      component: 'DynamicSelect',
      required: false,
      colProps: {
        span: 13,
      },
      ifShow: false,
      componentProps: () => {
        return {
          value: [{ shareProto: '', size: 500, num: 1 }],
        };
      },
    },
    {
      field: 'fileSystemType',
      label: '文件系统',
      component: 'Select',
      ifShow: false,
      colProps: {
        span: 13,
      },
      componentProps: {
        options: [
          {
            label: '文件系统',
            value: 'obsfs',
          },
          {
            label: 'OBS',
            value: 'obs',
          },
        ],
      },
    },
    {
      field: 'mountPath',
      label: '挂载路径',
      component: 'Input',
      ifShow: false,
      colProps: {
        span: 13,
      },
      componentProps: {},
    },
    {
      field: 'notificationEnabled',
      label: '是否接受消息通知',
      component: 'RadioGroup',
      required: true,
      colProps: {
        span: 13,
      },
      componentProps: ({ formActionType }) => {
        return {
          options: [
            { name: '是', label: '是', value: 'true' },
            { name: '否', label: '否', value: 'false' },
          ],
          onChange: async (e) => {
            const { updateSchema } = formActionType;
            updateSchema({
              field: 'notificationType',
              ifShow: e.target.value === 'true',
              required: e.target.value === 'true',
            });
            updateSchema({
              field: 'notificationTerminal',
              ifShow: e.target.value === 'true',
              required: e.target.value === 'true',
            });
          },
        };
      },
    },
    {
      field: 'notificationType',
      label: '通知协议类型',
      component: 'RadioGroup',
      required: false,
      colProps: {
        span: 13,
      },
      componentProps: {
        options: [
          { name: '短信', label: '短信', value: 'sms' },
          { name: '邮件', label: '邮件', value: 'email' },
        ],
      },
      ifShow: false,
    },
    {
      field: 'notificationTerminal',
      label: '通知终端',
      component: 'Input',
      required: false,
      colProps: {
        span: 13,
      },
      componentProps: {
        placeholder: '请输入邮箱或者手机号',
      },
      ifShow: false,
    },
    {
      field: 'paymentType',
      label: '付费类型',
      component: 'RadioGroup',
      required: true,
      colProps: {
        span: 13,
      },
      componentProps: {
        options: [
          { name: '包周期', label: '包周期', value: 'prepaid' },
          { name: '按需付费', label: '按需付费', value: 'postpaid' },
        ],
      },
    },

    {
      field: 'duration',
      component: 'InputNumber',
      label: '购买时长（月）',
      required: true,
      colProps: {
        span: 13,
      },
      componentProps: {
        width: 50,
        min: 1,
      },
      ifShow: ({ values }) => {
        return Boolean(values.paymentType === 'prepaid');
      },
      rules: [
        {
          trigger: 'change',
          required: true,
          message: '请选择购买时长',
        },
      ],
    },
  ];
  return selectedAppSchema.concat(tenantStep1Schema as any);
}
